test(v0952): robust capture of cli print output#34
Draft
HugoMachadoRodrigues wants to merge 1 commit into
Draft
Conversation
The two assertions on lines 86-87 of tests/testthat/test-v0952-vignette-pt.R
captured cli output via capture.output(type = "message"). That taps stderr,
which works in non-interactive R (CI) but silently returns empty when an
interactive session (RStudio, knitr renderer) has installed a custom
cli.default_handler that re-routes the message before it reaches stderr.
combined ended up empty and grepl("AC", combined) / grepl("CM", combined)
returned FALSE.
Force the default cli handler off via withr::local_options() and capture
the cli stream with testthat::capture_messages(), which uses
withCallingHandlers() to intercept the message condition directly. The
test now passes under both modes -- including a session that pre-installs
a hostile cli.default_handler -- while CI remains green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tests/testthat/test-v0952-vignette-pt.R:86-87failed locally onRscript -e 'pkgload::load_all(".") + testthat::test_dir(...)'while passing on CI.combinedcharacter vector was empty becausecapture.output(print(res), type = "message")taps stderr, but interactive R sessions (RStudio, knitr) sometimes install acli.default_handlerthat re-routes the message before it reaches stderr.cli.default_handler = NULLfor the test scope viawithr::local_options()and reads cli output throughtestthat::capture_messages(), which useswithCallingHandlers()to catch the message condition directly. Verified the new test passes both under default env AND under a session that pre-installs a hostilecli.default_handlerthat swallows everything (a stronger version of the failure mode the user hit). Confirmed the OLD test fails with the exact same error messages reported when run under that hostile env.Test plan
Rscript -e 'pkgload::load_all("."); testthat::test_dir("tests/testthat", filter="v0952")'-> 18/18 passoptions(cli.default_handler = function(msg) invisible(NULL))pre-installed -> 18/18 pass on the new test (and 16/18 on the old test, matching the original failure report)🤖 Generated with Claude Code